package main

import (
	"bufio"
	"fmt"
	"net"
)

func process(conn net.Conn)  {
	defer func(conn net.Conn) {
		err := conn.Close()
		if err != nil {
			fmt.Println(err)
		}
	}(conn)

	for  {
		reader:=bufio.NewReader(conn)
		var buf [128]byte
		n,err:=reader.Read(buf[:])
		if err!=nil {
			fmt.Println(err);break
		}
		recvStr := string(buf[:n])
		fmt.Println("收到recvStr：",recvStr)
		conn.Write([]byte(recvStr))
	}

}

func main() {
	listen,err:=net.Listen("tcp","127.0.0.1:20000")
	if err != nil {
		fmt.Println(err);return
	}
	for  {
		conn,err:=listen.Accept()
		if err != nil {
			fmt.Println(err);continue
		}
		go process(conn)
	}
}
